package com.spc.car.web.log.module;

import com.spc.car.common.utils.spring.SpringUtils;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.entity.log.LogProjectOperation;
import com.spc.car.data.entity.project.ProjectDoc;
import com.spc.car.data.service.project.ProjectDocService;
import com.spc.car.web.log.ProjectAction;
import com.spc.car.web.log.annotation.LogProjectAction;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;

public class LogProjectDoc extends BaseLogModule<LogProjectOperation, LogProjectAction> {
    public ProjectDocService docService;

    public LogProjectDoc() {
        this.docService = SpringUtils.getBean(ProjectDocService.class);
    }

    public LogProjectOperation toLogRecord(LogProjectAction logProjectAction,
                                           Map<String, Object> paramsMap,
                                           AjaxResult result) {
        LogProjectOperation logProjectOperation = new LogProjectOperation();

        ProjectAction[] projectActions = logProjectAction.action();
        ProjectDoc doc = null;
        if (StringUtils.isNotBlank(logProjectAction.idForPost())
                && Objects.nonNull(paramsMap.get(logProjectAction.idForPost()))) {
            doc = docService.getById((Long)paramsMap.get(logProjectAction.idForPost()));
        }

        for (ProjectAction projectAction: projectActions) {
            if (projectAction.equals(ProjectAction.ADD_DOC)
                    && null == doc) {
                logProjectOperation.setProjectTenderingSectionCode((String)paramsMap.get("projectTenderingSectionCode"));
                logProjectOperation.setProjectTenderingCode((String)paramsMap.get("projectTenderingCode"));
                logProjectOperation.setRemark(projectAction.getName()
                            + "[" + paramsMap.get("docName") + "]");
                break;

            } else if (projectAction.equals(ProjectAction.UPDATE_DOC) && null != doc) {
                logProjectOperation.setProjectTenderingSectionCode(doc.getProjectTenderingSectionCode());
                logProjectOperation.setProjectTenderingCode(doc.getProjectTenderingCode());
                logProjectOperation.setRemark(projectAction.getName()
                        + "[" + doc.getDocName() + "]");
                break;
            } else if (projectAction.equals(ProjectAction.DELETE_DOC) && null != doc) {
                logProjectOperation.setProjectTenderingSectionCode(doc.getProjectTenderingSectionCode());
                logProjectOperation.setProjectTenderingCode(doc.getProjectTenderingCode());
                logProjectOperation.setRemark(projectAction.getName()
                        + "[" + doc.getDocName() + "]");
                break;
            }
        }

        return logProjectOperation;
    }
}
